package com.henu.ocr.api.service.serviceImpl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.henu.ocr.api.model.DataDistributionRequestModel;
import com.henu.ocr.api.model.DataDistributionResponseModel;
import com.henu.ocr.entity.Company;
import com.henu.ocr.entity.User;
import com.henu.ocr.service.CompanyService;
import com.henu.ocr.service.UserService;
import com.henu.ocr.util.EncodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Optional;

@Slf4j
@Service
@SuppressWarnings("all")
public class DataDistributionServiceImpl {
    @Resource
    private CompanyService companyService;
    @Resource
    private UserService userService;
    @Resource
    private RabbitTemplate rabbitTemplate;

    public DataDistributionResponseModel dealWithPro(DataDistributionRequestModel model) {
        String busiDataId = String.valueOf(IdUtil.getSnowflakeNextId());
        return DataDistributionResponseModel.Ok(model, busiDataId);
    }

    public DataDistributionResponseModel dealWithTrader(DataDistributionRequestModel model) {
        return null;
    }

    public DataDistributionResponseModel dealWithPsn(DataDistributionRequestModel model) {
        try {
            boolean isSuccess = false;
            //下面参数没有添加完整
            Long busiDataId = (new Snowflake()).nextId();

            User user = User.builder()
                    .userId(busiDataId)
                    .username((String) model.get("pk_psnbasdoc"))
                    .password((String) EncodeUtil.encodePassword(model.get("userpwd")))
                    .realname((String) model.get("psnname"))
                    .userPhone((String) model.get("mobile"))
                    .email((String) model.get("email"))
                    .pkDeptdoc((String) model.get("pk_deptdoc"))
                    .psncode((String) model.get("psncode"))
                    .psnScopeName((String) model.get("psnscopname"))
                    .certid((String) model.get("certid"))
                    .build();
            isSuccess = userService.save(user);
            return isSuccess ? DataDistributionResponseModel.Ok(model,String.valueOf( busiDataId))
                    : DataDistributionResponseModel.Fail(model);
//            }
//            return DataDistributionResponseModel.Fail(model);
        } catch (Exception e) {
            return DataDistributionResponseModel.Exception(e, model);
        }
    }

    public DataDistributionResponseModel dealWithDept(DataDistributionRequestModel model) {
        return null;
    }

    @Transactional
    public DataDistributionResponseModel dealWithOrgWithDetails(DataDistributionRequestModel model) {
        try {
            boolean isSuccess = false;
            String fathercorp = Optional.ofNullable(model.get("fathercorp")).orElse(String.valueOf(0));
            Company company = Company.builder()
                    .mdmCode((String) model.get("mdm_code"))
                    .mdmPkCorp((String) model.get("pk_corp"))
                    .companyName((String) model.get("unitname"))
                    .build();
            long start1 = System.currentTimeMillis();

            isSuccess = companyService.save(company);
            long start2 = System.currentTimeMillis();

            //后期可以加上重试？
            //加上redis和mq，失败给两次机会
            //暂时不考虑mq了，先上java 线程池吧
            if (isSuccess) {
                companyService.completeCompanyDetailsAsync(company, fathercorp);
//                Company finalCompany = company;
//                rabbitTemplate.convertAndSend(CommonConstant.EXCHANGE_NAME, CommonConstant.ROUTING_KEY, (new HashMap<String, Object>() {{
//                    put("company", finalCompany);
//                    put("fathercorp", fathercorp);
//                }}));
                company = companyService.findCompanyByMdmCode(model.get("mdm_code"));
                String busiDataId = String.valueOf(company.getCompanyId());
                long start3 = System.currentTimeMillis();
                log.debug("save:{},find:{},total:{}", start2 - start1, start3 - start2, start3 - start1);
                return DataDistributionResponseModel.Ok(model, busiDataId);
            }
            return DataDistributionResponseModel.Fail(model);
        } catch (Exception e) {
            return DataDistributionResponseModel.Exception(e, model);
        }
    }
}